package com.lancoo.ccas52.arrangecourse.AlgDataSource.dataprocess.Impl;

import com.lancoo.ccas52.arrangecourse.AlgDataSource.baseentity.College;
import com.lancoo.ccas52.arrangecourse.AlgDataSource.baseentity.MajorGrade;
import com.lancoo.ccas52.arrangecourse.AlgDataSource.common.Constants;
import com.lancoo.ccas52.arrangecourse.AlgDataSource.dataprocess.MajorGradeDataService;
import com.lancoo.ccas52.pojo.vo.Campus;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description 初始化院系信息
 * @Author Fortysun
 * @Date 2023/4/11 10:20
 **/
public class MajorGradeDataServiceImpl implements MajorGradeDataService {
    //完整院系信息
    private HashMap<String, List<MajorGrade>> majorGradeMap;
    //基础院系信息；
    private List<College> collegeList;
    //校区信息
    private List<Campus> campusList;

    public MajorGradeDataServiceImpl() {

        //初始化院系信息
        collegeList = initCollegeList();
        //初始化校区信息
        campusList = initCampusList();
        //初始化专业/年级信息；
        majorGradeMap = initMajorGradeLisMap(collegeList, campusList);
    }

    /**
     * 初始化校区数据
     * @return
     */
    private List<Campus> initCampusList() {
        List<Campus> campusList = new ArrayList<>();
        for (int i = 0; i < Constants.CAMPUS_NUM; i++) {
            campusList.add(new Campus(
                    "campus" + (i+1),
                    "校区" + (i +1)
            ));
        }
        return campusList;
    }

    /**
     * 初始化专业/年级信息
     * @param collegeList 院系列表
     * @param campusList 校区列表
     * @return
     */
    private HashMap<String, List<MajorGrade>> initMajorGradeLisMap(List<College> collegeList,
                                                                 List<Campus> campusList) {
        //定义存储的map，使用collegeid作为键
        HashMap<String, List<MajorGrade>> majorGradeMap = new HashMap<>();
        //院系信息遍历的索引
        int collegeIndex = 0;
        for (College college : collegeList) {
            List<MajorGrade> majorGradeList = new ArrayList<>();
            for (int i = 0; i < Constants.GRADE_TYPE; i++) {
                for (int j = 0; j < Constants.COLLEGE_MAJOR_NUM; j++) {
                    int index = collegeIndex*Constants.GRADE_TYPE*Constants.COLLEGE_MAJOR_NUM + (i+1)*(j+1);
                    //获取校区信息
                    Campus campus = campusList.get(index%Constants.CAMPUS_NUM);
                    //生成专业id
                    String majorId = college.getCollegeId() + "-major" + (j +1);
                    //生成年级id
                    String gradeId = "grade"+(i+1);
                    //生成专业年级信息，并加入列表
                    majorGradeList.add(new MajorGrade(college.getCollegeId(),
                            campus.getCampusId(),
                            campus.getCampusName(),
                            majorId,
                            college.getColleageName() + "-专业" + (j+1),
                            gradeId,
                            2020+i + "级",
                            getStudentIdsList(majorId, gradeId)));

                }
            }
            //将列表数据，按照院系进行分开存储
            majorGradeMap.put(college.getCollegeId(), majorGradeList);
            collegeIndex++;
        }

        return majorGradeMap;
    }

    /**
     * 获取每个院系的，每个专业/年级对应的班级的，学生id字符串列表信息；
     * @param majorId 专业id
     * @param gradeId 年级id
     * @return
     */
    private List<String> getStudentIdsList(String majorId, String gradeId) {
        //学生id，前缀
        String idPrefix = majorId + gradeId;
        List<String> studentIdsList = new ArrayList<>();
        for (int i = 0; i < Constants.MAJOR_GRADE_CLASS_NUM; i++) {
            String studentIds = "";
            for (int j = 0; j < Constants.CLASS_STUDENT_NUM; j++) {
                String studentId = idPrefix + "student" + (i*Constants.CLASS_STUDENT_NUM + j+1);
                studentIds += studentId + ",";
            }
            //去掉末尾的逗号
            studentIds = studentIds.substring(0, studentIds.length()-1);
            //将拼接好的学生id字符串，存入列表
            studentIdsList.add(studentIds);
        }
        return studentIdsList;
    }

    /**
     * 获取院系列表信息；
     * @return
     */
    @Override
    public HashMap<String, List<MajorGrade>> getMajorGradeMap() {
        return this.majorGradeMap;
    }


    /**
     * 返回校区信息
     * @return
     */
    @Override
    public List<Campus> getCampusList() {
        return this.campusList;
    }

    /**
     * 初始化院系信息
     * @return
     */
    private List<College> initCollegeList() {
        collegeList = new ArrayList<>();
        for (int k = 0; k < Constants.COLLEGE_NUM; k++) {
            collegeList.add(new College(
                    "college" + (k+1),
                    "学院" + (k + 1)
            ));
        }

        return collegeList;
    }

    /**
     * 获取基础院系信息
     * @return
     */
    @Override
    public List<College> getCollegeList() {
        return this.collegeList;
    }
}
